package com.example.supermarket.controller;

import com.example.supermarket.pojo.Result;
import com.example.supermarket.pojo.User;
import com.example.supermarket.service.UserService;
import com.example.supermarket.utils.JwtUtil;
import com.example.supermarket.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<String> login(@RequestBody User user){
        User u=userService.login(user);
        if(u==null){
            return Result.error("账号或密码错误");
        }
        Map<String, Object> claims=new HashMap<>();
        claims.put("userId",u.getUserId());
        String token= JwtUtil.genToken(claims);
        return Result.success(token);
    }

    @GetMapping("/findRole")
    public Result<User> findRole(){
        User u=userService.findRole();
        return Result.success(u);
    }
}
